package com.adpython.dao.jdoDao;

import java.util.List;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.springframework.orm.jdo.support.JdoDaoSupport;

import com.adpython.dao.UsersDao;
import com.adpython.domain.Users;

public class UsersJdoDao extends JdoDaoSupport implements UsersDao {
	
	public Users getById(Long id){
		PersistenceManager pm = super.getPersistenceManager();
		Users user=null;
		Extent r = pm.getExtent (Users.class, false);
		String filter = "id=="+id;
		Query query = pm.newQuery(r,filter);
		List<Users> list = (List<Users>) query.execute();
		if(!list.isEmpty())
			user = list.get(0);
		return user;
	}
	
	public List<Users> getAll(){
		PersistenceManager pm = getPersistenceManager();
		Query query = pm.newQuery(Users.class);
		List<Users> list = (List<Users>) query.execute();
		return list;
	}
	
	public void save(Users user){
		getJdoTemplate().makePersistent(user);
	}

	public void removeById(Long id){
		PersistenceManager pm = getPersistenceManager();
		Extent r = pm.getExtent (Users.class, false);
		String filter = "id=="+id;
		Query query = pm.newQuery(r,filter);
		List<Users> list = (List<Users>) query.execute();
		if(!list.isEmpty()){
			Users delUser = list.get(0);
			pm.deletePersistent(delUser);
		}
	}
	
}
